package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Goods;
import com.example.entity.GoodsFeedback;
import com.example.entity.Order;
import com.example.entity.User;
import com.example.entity.vo.OrderExpVo;
import com.example.service.GoodsFeedbackService;
import com.example.service.GoodsService;
import com.example.service.OrderService;
import com.example.service.UserService;
import com.example.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "订单管理")
@CrossOrigin
@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsFeedbackService goodsFeedbackService;

    @ApiOperation(value = "分页查询订单")
    @GetMapping("getOrder/{current}/{limit}")
    public R getOrder(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit
    ) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        //实现分页
        Page<Order> orderPage = new Page<>(current, limit);
        orderService.page(orderPage, wrapper);
        //获取分页后数据
        long total = orderPage.getTotal();
        List<Order> list = orderPage.getRecords();

        return R.ok().data("total", total).data("list", list);
    }

    @ApiOperation(value = "分页带条件查询订单")
    @PostMapping("getOrder/{current}/{limit}")
    public R searchOrder(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit,
            @RequestBody(required = false) Order order
    ) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(order)) {
            if (!StringUtils.isEmpty(order.getUserIdFrom())) {
                wrapper.eq("user_id_from", order.getUserIdFrom());
            }
            if (!StringUtils.isEmpty(order.getUserIdTo())) {
                wrapper.eq("user_id_to", order.getUserIdTo());
            }
            if (!StringUtils.isEmpty(order.getOrderType())) {
                wrapper.eq("order_type", order.getOrderType());
            }
        }
        wrapper.orderByDesc("gmt_create");
        //实现分页
        Page<Order> orderPage = new Page<>(current, limit);
        orderService.page(orderPage, wrapper);
        //获取分页后数据
        long total = orderPage.getTotal();
        List<Order> list = orderPage.getRecords();

        return R.ok().data("total", total).data("list", list);
    }

    @ApiOperation(value = "逻辑删除订单")
    @DeleteMapping("deleteOrder/{id}")
    public R delteOrder(@PathVariable String id) {
        boolean b = orderService.removeById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "修改订单状态")
    @PostMapping("updateOrder")
    public R updateOrder(@RequestBody Order order) {
        //查原来的积分
        User user = userService.getById(order.getUserIdFrom());
        int credit = Integer.parseInt(user.getCredit());
        //积分加10
        user.setCredit(String.valueOf(credit + 10));
        GoodsFeedback goodsFeedback = new GoodsFeedback();
        goodsFeedback.setUserIdFrom(order.getUserIdFrom());
        goodsFeedback.setUserIdTo(order.getUserIdTo());
        goodsFeedbackService.save(goodsFeedback); //添加待反馈表
        boolean b1 = userService.updateById(user);
        boolean b = orderService.updateById(order);
        if (b && b1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "添加物流单号")
    @PostMapping("addOrderExp")
    public R addOrderExp(@RequestBody OrderExpVo orderExpVo) {
        Order order = new Order();
        order.setOrderId(orderExpVo.getOrderId());
        order.setExpressId(orderExpVo.getExpressId());
        boolean b = orderService.updateById(order);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    @ApiOperation(value = "收书订单生成")
    @PostMapping("addOrder")
    public R addOrder(
            @ApiParam(name = "goods", value = "物品信息", required = true) @RequestBody Order order //user_id_from , user_id_to , add_id_from , add_id_to , goods_id
    ) {
        if (!StringUtils.isEmpty(order)) {
            //判断用户是否为贫困用户
            User user = userService.getById(order.getUserIdTo());
            Goods goods2 = goodsService.getById(order.getGoodsId());
            if (goods2.getIsSell() == 0) {

                if (user.getLevel() == 2) {
                    String userIdFrom = order.getUserIdFrom();
                    String userIdTo = order.getUserIdTo();
                    String addIdFrom = order.getAddIdFrom();
                    String addIdTo = order.getAddIdTo();
                    String goodsId = order.getGoodsId();
                    if (!StringUtils.isEmpty(userIdFrom) && !StringUtils.isEmpty(userIdTo) && !StringUtils.isEmpty(addIdFrom) && !StringUtils.isEmpty(addIdTo) && !StringUtils.isEmpty(goodsId)) {
                        //添加订单
                        boolean b = orderService.save(order);
                        //物品 is_sell 状态改为：已出
                        Goods goods = new Goods();
                        goods.setGoodsId(goodsId);
                        goods.setIsSell(1);
                        boolean b1 = goodsService.updateById(goods);
                        if (b && b1) {
                            return R.ok();
                        } else {
                            return R.error();
                        }
                    }
                } else {
                    return R.error().message("您不是贫困用户");
                }
            } else {
                return R.error().message("物品不存在");
            }

        }
        return R.error().message("订单信息为空或不全");
    }

    @ApiOperation(value = "捐书订单生成")
    @PostMapping("addDonaOrder")
    public R addAccOrder(
            @ApiParam(name = "goods", value = "物品信息", required = true) @RequestBody Order order //user_id_from , user_id_to , add_id_from , add_id_to , goods_id
    ) {
        if (!StringUtils.isEmpty(order)) {
            //判断用户是否为贫困用户
            User user = userService.getById(order.getUserIdTo());
            Goods goods2 = goodsService.getById(order.getGoodsId());
            if (goods2.getIsSell() == 0) {
                String userIdFrom = order.getUserIdFrom();
                String userIdTo = order.getUserIdTo();
                String addIdFrom = order.getAddIdFrom();
                String addIdTo = order.getAddIdTo();
                String goodsId = order.getGoodsId();
                if (!StringUtils.isEmpty(userIdFrom) && !StringUtils.isEmpty(userIdTo) && !StringUtils.isEmpty(addIdFrom) && !StringUtils.isEmpty(addIdTo) && !StringUtils.isEmpty(goodsId)) {
                    //添加订单
                    boolean b = orderService.save(order);
                    //物品 is_sell 状态改为：已出
                    Goods goods = new Goods();
                    goods.setGoodsId(goodsId);
                    goods.setIsSell(1);
                    boolean b1 = goodsService.updateById(goods);
                    if (b && b1) {
                        return R.ok();
                    } else {
                        return R.error();
                    }
                }

            } else {
                return R.error().message("物品不存在");
            }

        }
        return R.error().message("订单信息为空或不全");
    }
}
